package com.liuchuanzheng.lcz_wanandroid.module.splash.activity

import android.animation.Animator
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import com.airbnb.lottie.LottieAnimationView
import com.liuchuanzheng.lcz_wanandroid.R
import com.liuchuanzheng.lcz_wanandroid.module.main.activity.MainActivity
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        var animation_view = findViewById<LottieAnimationView>(R.id.animation_view)
        var animation_view2 = findViewById<LottieAnimationView>(R.id.animation_view2)
        animation_view2.loop(false)
        findViewById<Button>(R.id.btn_go).setOnClickListener {
            animation_view.visibility = View.GONE
            animation_view2.visibility = View.VISIBLE
            animation_view2.playAnimation()
            findViewById<Button>(R.id.btn_go).visibility = View.GONE
        }
        animation_view2.addAnimatorListener(object : Animator.AnimatorListener{
            /**
             *
             * Notifies the start of the animation.
             *
             * @param animation The started animation.
             */
            override fun onAnimationStart(animation: Animator?) {

            }

            /**
             *
             * Notifies the end of the animation. This callback is not invoked
             * for animations with repeat count set to INFINITE.
             *
             * @param animation The animation which reached its end.
             */
            override fun onAnimationEnd(animation: Animator?) {
                startActivity(Intent(this@SplashActivity, MainActivity::class.java))
                finish()
            }

            /**
             *
             * Notifies the cancellation of the animation. This callback is not invoked
             * for animations with repeat count set to INFINITE.
             *
             * @param animation The animation which was canceled.
             */
            override fun onAnimationCancel(animation: Animator?) {

            }

            /**
             *
             * Notifies the repetition of the animation.
             *
             * @param animation The animation which was repeated.
             */
            override fun onAnimationRepeat(animation: Animator?) {

            }

        })
        //协程的写法。
//        GlobalScope.launch {
//            delay(2000)
//            startActivity(Intent(this@SplashActivity, MainActivity::class.java))
//            finish()
//        }

    }
}